//
//  LZAreaCodeViewController.m
//  laziz_Merchant
//
//  Created by xbnzd on 17/3/16.
//  Copyright © 2017年 XBN. All rights reserved.
//

#import "LZAreaCodeViewController.h"
#import "UIViewController+NavigationItemSetting.h"
#import "UIView+Rotation.h"
#import "LZUserInfoPresenter.h"
#import "LZAreaCodeHeadView.h"
#import "LZAreaCodeCell.h"
#import "extobjc.h"

@interface LZAreaCodeViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (strong, nonatomic) NSArray *dataArr;

@property (strong, nonatomic) LZUserInfoPresenter *userPresenter;

@end

@implementation LZAreaCodeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self configureUI];
    
    [self configureNavBar];
    
    [self getCodeList];
}

#pragma mark - private
- (void)getCodeList
{
     @weakify(self);
     [self.userPresenter getAreaCodeSuccess:^(NSArray *listArr) {
         
         @strongify(self);
         self.dataArr = listArr;
         [self.tableView reloadData];
         
     } Failure:^(NSString *fail) {
         
     }];
}

- (void)navbarBackButtonClickAction
{
    [self dismissViewControllerAnimated:YES completion:^{
        
    }];
}

#pragma mark - UI & data
- (void)configureUI
{
    self.umLogPageViewName = @"城市选择界面";
    
    self.title = LZLocalizedString(@"login_select_area_code", nil);
    
    _userPresenter = [[LZUserInfoPresenter alloc] init];
    
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    
    [self.tableView registerNib:[UINib nibWithNibName:@"LZAreaCodeCell" bundle:nil] forCellReuseIdentifier:@"cell"];
    [self.tableView registerNib:[UINib nibWithNibName:@"LZAreaCodeHeadView" bundle:nil] forHeaderFooterViewReuseIdentifier:@"head"];
    
}

- (void)configureNavBar
{
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    btn.frame = CGRectMake(0, 0, 60, 40);
    [btn addTarget:self action:@selector(navbarBackButtonClickAction) forControlEvents:UIControlEventTouchUpInside];
    UIImage *image_back = [UIImage imageNamed:@"lz_back_en"];
    [btn setImage:image_back forState:UIControlStateNormal];
    btn.imageEdgeInsets = UIEdgeInsetsMake(0, 0, 0, CGRectGetWidth(btn.bounds)-image_back.size.width);
    if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 9.0) {
        [btn setLanguageRotaion];
    }
    
    [self navigationItemSetting:@[btn] type:NAVIGATIONITEMSETTING_LEFT];

}


#pragma mark - tableview
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return self.dataArr.count;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    NSDictionary *dic = self.dataArr[section];
    NSArray *arr = dic[@"list"];
    return arr.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 50;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    LZAreaCodeHeadView *headV = [tableView dequeueReusableHeaderFooterViewWithIdentifier:@"head"];
    headV.backgroundColor = [UIColor redColor];
    NSDictionary *dic = self.dataArr[section];
    headV.headCodeLabel.text = dic[@"letter"];
    
    return headV;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 40;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    LZAreaCodeCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    NSDictionary *dic = self.dataArr[indexPath.section];
    NSArray *arr = dic[@"list"];
    NSDictionary *dicRow = arr[indexPath.row];
    
    if ([LZLanguageManager shareInstance].getLanguageType == LanguageType_En) {
        [cell loadAreaName:dicRow[@"enName"] areaCode:dicRow[@"countryCode"]];
    } else {
        [cell loadAreaName:dicRow[@"arName"] areaCode:dicRow[@"countryCode"]];
    }
    
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    NSDictionary *dic = self.dataArr[indexPath.section];
    NSArray *arr = dic[@"list"];
    NSDictionary *dicRow = arr[indexPath.row];
    NSString *codeStr = dicRow[@"countryCode"];
    
    if (self.SelectedAreaCodeBlock) {
        self.SelectedAreaCodeBlock(codeStr);
        
        [self navbarBackButtonClickAction];
    }
}


@end
